#include <allegro.h>
#include <stdlib.h>
#include <stdio.h>
#include<time.h>

#include "menu.h"
#include "opening.h"
#include "clavier.h"

void close_button_handler(void);
volatile int close_button_pressed = FALSE;
void fermer (picture pic,music mus);
int main()
{
    positions position;
    picture pic;
    music mus;
    vi v[40];
    cais dinar [40];

    int avancer=0,l=10,life=0, money=0,poskarhba=0,p=0,by=300,nv=0,nd=0;
    srand(time(NULL));
    p=initialisation();
    images(&pic,&mus);
    if (pic.Image == NULL && pic.Background == NULL)
    {
        allegro_message("could not load images");
        allegro_exit();
    }
    play_sample(mus.sample, 255, 128, 1000, FALSE);
    set_close_button_callback(close_button_handler);
    menu(pic);
    /**************************************************************************************************/
    position.x1=SCREEN_W/2;
    position.y1=SCREEN_H/2+100;
    premiere_vue(pic ,position,v,dinar,l);

    poskarhba=SCREEN_H-250;
    while (close_button_pressed!=TRUE)

    {
        /**************************************************************************************************/
        money=0;
        nv=20;
        nd=20;
        if (key[KEY_LEFT]  && avancer!=0)
        {
            gauche (&pic,&avancer ,position,&poskarhba,dinar,v,&money,mus);

        }
        /**************************************************************************************************/
        if (key [KEY_RIGHT] && avancer !=26000)

        {
            droite (&avancer ,position,&pic,v,dinar,&money,mus,&poskarhba,&nv,&nd);
        }

        /**************************************************************************************************/

        if((key[KEY_SPACE])&&(key[KEY_RIGHT]))
        {

            sauter (pic,position,poskarhba,&avancer,&by);
        }
    }

    /**************************************************************************************************/


    fermer (pic,mus);
    return 0;

}
END_OF_MAIN();
/**************************************************************************************************/
void close_button_handler(void)
{
    close_button_pressed = TRUE;
}
END_OF_FUNCTION(close_button_handler);


/**************************************************************************************************/

/**************************************************************************************************/
void fermer (picture pic,music mus)
{
    destroy_bitmap(pic.Image);
    destroy_bitmap(pic.Imageri);
    destroy_bitmap(pic.Buffer);
    destroy_bitmap(pic.Imagel);
    destroy_bitmap(pic.Imagele);
    destroy_bitmap(pic.menu);
    destroy_bitmap(pic.Background);
    destroy_sample(mus.sample);
    destroy_sample(mus.sample2);
    destroy_sample(mus.sample3);
    destroy_sample(mus.sample4);
}


